package com.dameng.jianyouquan.jobhunter.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dameng.jianyouquan.R;
import com.dameng.jianyouquan.api.Constant;
import com.dameng.jianyouquan.bean.CommonBean;
import com.dameng.jianyouquan.api.NetWorkManager;
import com.dameng.jianyouquan.mvp.view.activity.LoginActivity;
import com.dameng.jianyouquan.utils.SpUtils;

import java.util.Objects;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class Myfragment extends Fragment  {



    private RecyclerView recycleView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(inflater.getContext(), R.layout.fragment_my, null);
        recycleView=view.findViewById(R.id.recycleView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4, GridLayoutManager.VERTICAL, false);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(gridLayoutManager);
        recycleView.setAdapter(new GridAdapter());

        view.findViewById(R.id.civ).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exit();
            }
        });

        return view;
    }

    private void exit() {
        NetWorkManager.getService()
                .getLogout()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(CommonBean commonBean) {
                        int code = commonBean.getCode();
                        if (code==200){
                            Context context = getContext();
                            if (context!=null){
                                SpUtils.setValue(context, Constant.SP_UID,"");
                                SpUtils.setValue(context, Constant.SP_TOKEN,"");
                                SpUtils.setValueInt(context, Constant.SP_ROLE_ID,0);
                                SpUtils.setValueInt(context, Constant.SP_STATUS,0);
                                SpUtils.setValue(context, Constant.SP_USERNAME,"");
                                SpUtils.setValue(context, Constant.SP_USERPWD,"");
                                SpUtils.setValue(context, Constant.SP_USERPWD,"");
                                SpUtils.setValue(context, Constant.SP_WXOPENID,"");
                                Objects.requireNonNull(getActivity()).finish();
                                startActivity(new Intent(getActivity(), LoginActivity.class));
                            }
                        }
                    }
                });

    }

    private class GridAdapter extends RecyclerView.Adapter<GridHolder> {
        @NonNull
        @Override
        public GridHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(getContext(), R.layout.item_grid, null);
            return new GridHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull GridHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 5;
        }
    }


    private class GridHolder extends RecyclerView.ViewHolder{

        public GridHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
